/**
 * @class Ext.data.sqlResultJsonReader
 * 用于解析后台直接用sql语句查询出结果 create by hezn
 * @param {Object} meta Metadata configuration options
 * @param {Object[]/Ext.data.Record constructor} recordType The record definition of the data record to produce.
 */
Ext.data.sqlJsonReader = function(meta, recordType){
    Ext.data.sqlJsonReader.superclass.constructor.call(this, meta, recordType);
};
Ext.extend(Ext.data.sqlJsonReader, Ext.data.DataReader, {
    /**
     * This method is only used by a DataProxy which has retrieved data from a remote server.
     * @param {Object} response The XHR object which contains the JSON data in its responseText.
     * @return {Object} data A data block which is used by an Ext.data.Store object as
     * a cache of Ext.data.Records.
     */
    read : function(response){
        var json = response.responseText;
       
        var o = eval("("+json+")");
        if(!o) {
            throw {message: "JsonReader.read: Json object not found"};
        }
        return this.readRecords(o);
    },

	/**
	 * @ignore
	 */
    simpleAccess: function(obj, subsc) {
    	return obj[subsc];
    },

	/**
	 * @ignore
	 */
    getJsonAccessor: function(){
        var re = /[\[\.]/;
        return function(expr) {
            try {
                return(re.test(expr))
                    ? new Function("obj", "return obj." + expr)
                    : function(obj){
                        return obj[expr];
                    };
            } catch(e){}
            return Ext.emptyFn;
        };
    }(),

    /**
     * Create a data block containing Ext.data.Records from an XML document.
     * @param {Object} o An object which contains an Array of row objects in the property specified
     * in the config as 'root, and optionally a property, specified in the config as 'totalProperty'
     * which contains the total size of the dataset.
     * @return {Object} data A data block which is used by an Ext.data.Store object as
     * a cache of Ext.data.Records.
     */
    readRecords : function(o){
        /**
         * After any data loads, the raw JSON data is available for further custom processing.
         * @type Object
         */
        this.jsonData = o;
        
        var s = this.meta, Record = this.recordType,


            f = Record.prototype.fields, fi = f.items, fl = f.length;
         var sid=this.meta ? this.meta.id : null;
         var sumType='0';  // 合计类型     hezn 
//      Generate extraction functions for the totalProperty, the root, the id, and for each field
        if (!this.ef) {
            if(s.totalProperty) {
	            this.getTotal = this.getJsonAccessor(s.totalProperty);
	        }
	        if(s.successProperty) {
	            this.getSuccess = this.getJsonAccessor(s.successProperty);
	        }
	        this.getRoot = s.root ? this.getJsonAccessor(s.root) : function(p){return p;};
            this.getSumType=this.getJsonAccessor('sumType');   // sumType     统计类型

	        if (s.id) {
	        	var g = this.getJsonAccessor(s.id);
	        	this.getId = function(rec) {
	        		var r = g(rec);
		        	return (r === undefined || r === "") ? null : r;
	        	};
	        } else {
	        	this.getId = function(){return null;};
	        }
            this.ef = [];
            for(var i = 0; i < fl; i++){
                f = fi[i];
                var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
                this.ef[i] = this.getJsonAccessor(map);
            }
        }

    	var root = this.getRoot(o), c = root.length, totalRecords = c, success = true;
    	if(s.totalProperty){
            var v = parseInt(this.getTotal(o), 10);
            if(!isNaN(v)){
                totalRecords = v;
            }
        }
        if(s.successProperty){
            var v = this.getSuccess(o);
            if(v === false || v === 'false'){
                success = false;
            }
        }

       var sid = this.meta ? this.meta.id : null;
    	var recordType = this.recordType, fields = recordType.prototype.fields;
           var records = [];
           for(var i = 0; i < root.length; i++){
              
                    var n = root[i];
                    var values = {};
                    var id;
                   if(sid<0)id=i+1  //hezn 如果id为赋值 取索引值

                    else id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
                    if(id==null)id='hj';     //标示 合计行
                    for(var j = 0, jlen = fields.length; j < jlen; j++){
                        var f = fields.items[j];

                       // alert(j)
                        var k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
                        var v = n[k] !== undefined ? n[k] : f.defaultValue;
                        v = f.convert(v);
                        values[f.name] = v;
                    }
                    var record = new recordType(values, id);
                    record.json = n;
                    records[i] = record;
                }
	    return {
	        success : success,
	        records : records,
	        totalRecords : totalRecords
	    };

    }
});

